import { BaseScene } from './BaseScene'
import { GameUtils } from '../utils/GameUtils'

export class GameScene extends BaseScene {
  constructor(game) {
    super(game)
    
    // 游戏状态
    this.gameState = {
      isPlaying: false,
      currentPlayer: null,
      players: [],
      currentQuestion: null,
      timer: 30,
      remainingTime: 30,
      skipCount: 0,
      maxSkipCount: 2,
      diceValue: null
    }
    
    // 问题库
    this.questions = [
      { type: 'truth', content: '你最难忘的一次旅行是什么？' },
      { type: 'dare', content: '模仿一个动物叫声' },
      { type: 'truth', content: '你最尴尬的经历是什么？' },
      { type: 'dare', content: '唱一首歌的副歌部分' }
    ]
    
    // UI元素
    this.buttons = {
      rollDice: {
        x: this.width / 2 - 100,
        y: this.height / 2 - 50,
        width: 200,
        height: 50,
        text: '掷骰子'
      },
      skip: {
        x: 20,
        y: this.height - 70,
        width: 80,
        height: 40,
        text: '换题'
      },
      confirm: {
        x: this.width - 100,
        y: this.height - 70,
        width: 80,
        height: 40,
        text: '确认'
      }
    }
    
    // 初始化触摸事件
    this.initTouchEvents()
  }
  
  init() {
    // 初始化游戏状态
    this.gameState = {
      isPlaying: false,
      currentPlayer: null,
      players: [],
      currentQuestion: null,
      timer: 30,
      remainingTime: 30,
      skipCount: 0,
      maxSkipCount: 2,
      diceValue: null
    }
    
    // 开始游戏
    this.startGame()
  }
  
  update() {
    if (!this.gameState.isPlaying) return
    
    // 更新计时器
    if (this.gameState.remainingTime > 0) {
      this.gameState.remainingTime -= 1/60 // 假设60FPS
    } else {
      this.handleTimeUp()
    }
  }
  
  render(ctx) {
    // 绘制背景
    ctx.fillStyle = '#f0f0f0'
    ctx.fillRect(0, 0, this.width, this.height)
    
    // 绘制玩家信息
    this.renderPlayers(ctx)
    
    // 绘制骰子区域
    this.renderDiceArea(ctx)
    
    // 绘制问题/任务区域
    this.renderQuestionArea(ctx)
    
    // 绘制计时器
    this.renderTimer(ctx)
    
    // 绘制按钮
    this.renderButtons(ctx)
  }
  
  renderPlayers(ctx) {
    // 绘制玩家头像和名称
    this.gameState.players.forEach((player, index) => {
      const x = index === 0 ? 50 : this.width - 150
      const y = 50
      
      // 绘制头像背景
      ctx.fillStyle = '#4ECDC4'
      ctx.beginPath()
      ctx.arc(x, y, 30, 0, Math.PI * 2)
      ctx.fill()
      
      // 绘制玩家名称
      ctx.fillStyle = '#333'
      ctx.font = '16px Arial'
      ctx.textAlign = 'center'
      ctx.fillText(player.name, x, y + 50)
    })
  }
  
  renderDiceArea(ctx) {
    if (this.gameState.diceValue !== null) {
      // 绘制骰子
      ctx.fillStyle = '#FF6B35'
      ctx.fillRect(this.width/2 - 25, this.height/2 - 25, 50, 50)
      
      // 绘制骰子点数
      ctx.fillStyle = '#FFFFFF'
      ctx.font = '24px Arial'
      ctx.textAlign = 'center'
      ctx.fillText(this.gameState.diceValue.toString(), this.width/2, this.height/2 + 8)
    }
  }
  
  renderQuestionArea(ctx) {
    if (this.gameState.currentQuestion) {
      // 绘制问题背景
      ctx.fillStyle = '#FFFFFF'
      ctx.fillRect(50, this.height/2 + 50, this.width - 100, 100)
      
      // 绘制问题内容
      ctx.fillStyle = '#333'
      ctx.font = '18px Arial'
      ctx.textAlign = 'center'
      ctx.fillText(this.gameState.currentQuestion.content, this.width/2, this.height/2 + 100)
    }
  }
  
  renderTimer(ctx) {
    // 绘制计时器背景
    ctx.fillStyle = '#4ECDC4'
    ctx.fillRect(this.width/2 - 50, 20, 100, 30)
    
    // 绘制剩余时间
    ctx.fillStyle = '#FFFFFF'
    ctx.font = '20px Arial'
    ctx.textAlign = 'center'
    ctx.fillText(Math.ceil(this.gameState.remainingTime).toString(), this.width/2, 40)
  }
  
  renderButtons(ctx) {
    Object.values(this.buttons).forEach(button => {
      GameUtils.drawButton(
        ctx,
        button.x,
        button.y,
        button.width,
        button.height,
        button.text
      )
    })
  }
  
  initTouchEvents() {
    wx.onTouchStart(this.onTouchStart.bind(this))
  }
  
  onTouchStart(e) {
    const touch = e.touches[0]
    const x = touch.clientX
    const y = touch.clientY
    
    // 检查按钮点击
    Object.entries(this.buttons).forEach(([key, button]) => {
      if (GameUtils.isPointInRect(x, y, button.x, button.y, button.width, button.height)) {
        switch (key) {
          case 'rollDice':
            this.rollDice()
            break
          case 'skip':
            this.skipQuestion()
            break
          case 'confirm':
            this.confirmAnswer()
            break
        }
      }
    })
  }
  
  startGame() {
    this.gameState.isPlaying = true
    this.gameState.players = [
      { name: '玩家1', score: 0 },
      { name: '玩家2', score: 0 }
    ]
    this.gameState.currentPlayer = this.gameState.players[0]
    this.selectRandomQuestion()
  }
  
  rollDice() {
    // 生成1-6的随机数
    this.gameState.diceValue = Math.floor(Math.random() * 6) + 1
    
    // 根据骰子值选择玩家
    this.gameState.currentPlayer = this.gameState.players[this.gameState.diceValue % 2]
    
    // 重置计时器
    this.gameState.remainingTime = this.gameState.timer
  }
  
  selectRandomQuestion() {
    const randomIndex = Math.floor(Math.random() * this.questions.length)
    this.gameState.currentQuestion = this.questions[randomIndex]
  }
  
  skipQuestion() {
    if (this.gameState.skipCount < this.gameState.maxSkipCount) {
      this.gameState.skipCount++
      this.selectRandomQuestion()
    }
  }
  
  confirmAnswer() {
    // 切换玩家
    this.gameState.currentPlayer = this.gameState.players.find(p => p !== this.gameState.currentPlayer)
    
    // 重置状态
    this.gameState.diceValue = null
    this.gameState.remainingTime = this.gameState.timer
    this.selectRandomQuestion()
  }
  
  handleTimeUp() {
    // 时间到，自动切换到下一个玩家
    this.confirmAnswer()
  }
  
  destroy() {
    // 清理事件监听
    wx.offTouchStart()
  }
} 